package com.xnx3.banmahuishou.wangge.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xnx3.BaseVO;
import com.xnx3.banmahuishou.core.entity.Qishou;
import com.xnx3.banmahuishou.core.entity.QishouLeaveRecord;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.service.UserService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.util.Page;
import com.xnx3.j2ee.util.Sql;

/**
 * 骑手请假记录
 * @author 王晓龙
 */
@Controller(value="BanmahuishouWanggeQishouLeaveRecordController")
@RequestMapping("/wangge/qishouleaverecord/")
public class QishouLeaveRecordController extends BaseController {
	@Resource
	private SqlService sqlService;
	@Resource
	private UserService userService;
	
	/**
	 * 骑手请假记录列表
	 */
	@RequestMapping("list${url.suffix}")
	public String list(HttpServletRequest request,Model model){
		Sql sql = new Sql(request);
		sql.setSearchColumn(new String[]{"qishou_phone"});
		sql.appendWhere("wangge_userid = " + getUserId());
		sql.appendWhere("state = " + QishouLeaveRecord.STATE_UNDER);
		int count = sqlService.count("laji_qishou_leave_record", sql.getWhere());
		Page page = new Page(count, 15, request);
		sql.setSelectFromAndPage("SELECT * FROM laji_qishou_leave_record", page);
		sql.setDefaultOrderBy("laji_qishou_leave_record.id DESC");
		List<QishouLeaveRecord> list = sqlService.findBySql(sql, QishouLeaveRecord.class);
		
		ActionLogUtil.insert(request, "骑手请假记录", "第"+page.getCurrentPageNumber()+"页");
		
		model.addAttribute("page", page);
		model.addAttribute("list", list);
		return "/wangge/qishouleaverecord/list";
	}
	
	/**
	 * 同意审核
	 */
	@RequestMapping(value="argee.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO argee(@RequestParam(value = "id", required = false, defaultValue="0") int id, HttpServletRequest request){
		QishouLeaveRecord entity = sqlService.findById(QishouLeaveRecord.class, id);
		if(entity == null){
			return error("要同意的目标不存在");
		}
		entity.setState(QishouLeaveRecord.STATE_PASSED);
		sqlService.save(entity);
		
		ActionLogUtil.insertUpdateDatabase(request, entity.getId(), "同意请假", entity.toString());
		return success();
	}
	

	/**
	 * 拒绝审核
	 */
	@RequestMapping(value="refuse.json", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO refuse(@RequestParam(value = "id", required = false, defaultValue="0") int id, HttpServletRequest request){
		QishouLeaveRecord entity = sqlService.findById(QishouLeaveRecord.class, id);
		if(entity == null){
			return error("要拒绝的目标不存在");
		}
		entity.setState(QishouLeaveRecord.STATE_REJECTED);
		sqlService.save(entity);;
		
		ActionLogUtil.insertUpdateDatabase(request, entity.getId(), "拒绝请假", entity.toString());
		return success();
	}

}
